package com.yanqu.road.dao.impl.activity.wanyao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.wanyao.config.WanYaoMissionConfig;
import com.yanqu.road.entity.activity.wanyao.config.WanYaoMonsterKingConfig;
import com.yanqu.road.entity.activity.wanyao.config.WanYaoNpcConfig;
import com.yanqu.road.entity.activity.wanyao.config.WanYaoTalentConfig;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WanYaoConfigDaoImpl  extends TempDao {
    private static String getWanYaoTalentConfigSql = "select * from `t_s_activity_wanyao_talent` where `activityId`=?";
    public Map<Integer,Map<Integer,WanYaoTalentConfig>>  getWanYaoTalentConfigMap(int activityId) {
        Map<Integer,Map<Integer,WanYaoTalentConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWanYaoTalentConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WanYaoTalentConfig wanYaoTalentConfig = new WanYaoTalentConfig();
                wanYaoTalentConfig.setActivityId(rs.getInt("activityId"));
                wanYaoTalentConfig.setId(rs.getInt("id"));
                wanYaoTalentConfig.setType(rs.getInt("type"));
                wanYaoTalentConfig.setLevel(rs.getInt("level"));
                wanYaoTalentConfig.setUnlock(rs.getInt("unlock"));
                wanYaoTalentConfig.setCost(rs.getInt("cost"));
                wanYaoTalentConfig.setName(rs.getString("name"));
                wanYaoTalentConfig.setDesc(rs.getString("desc"));
                wanYaoTalentConfig.setTalentPara(rs.getString("talentPara"));
                wanYaoTalentConfig.setDisplayText(rs.getString("displayText"));

                List<Integer> paraList = StringUtils.stringToIntegerList(wanYaoTalentConfig.getTalentPara(), ";");
                wanYaoTalentConfig.setTalentParaList(paraList);

                map.putIfAbsent(wanYaoTalentConfig.getType(),new ConcurrentHashMap<>());
                map.get(wanYaoTalentConfig.getType()).put(wanYaoTalentConfig.getLevel(),wanYaoTalentConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getWanYaoMissionConfigSql = "select * from `t_s_activity_wanyao_mission` where `activityId`=?";
    public Map<Integer,WanYaoMissionConfig> getWanYaoMissionConfigMap(int activityId) {
        Map<Integer,WanYaoMissionConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWanYaoMissionConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WanYaoMissionConfig wanYaoMissionConfig = new WanYaoMissionConfig();
                wanYaoMissionConfig.setActivityId(rs.getInt("activityId"));
                wanYaoMissionConfig.setId(rs.getInt("id"));
                wanYaoMissionConfig.setName(rs.getString("name"));
                wanYaoMissionConfig.setMapId(rs.getInt("mapId"));

                wanYaoMissionConfig.setEnemyList(rs.getString("enemyList"));
                wanYaoMissionConfig.setEnemyIdList( StringUtils.stringToIntegerList(wanYaoMissionConfig.getEnemyList(),";") );

                wanYaoMissionConfig.setEnemyPower(rs.getString("enemyPower"));
                wanYaoMissionConfig.setEnemyPowerList(StringUtils.stringToLongList(wanYaoMissionConfig.getEnemyPower(),";"));

                wanYaoMissionConfig.setBossId(rs.getInt("bossId"));
                wanYaoMissionConfig.setBossPower(rs.getLong("bossPower"));

                wanYaoMissionConfig.setEnemyBean(rs.getString("enemyBean"));
                wanYaoMissionConfig.setEnemyFlowerList(StringUtils.stringToIntegerList(wanYaoMissionConfig.getEnemyBean(),";"));

                wanYaoMissionConfig.setBeanList(rs.getString("beanList"));
                wanYaoMissionConfig.setBoxFlowerList(StringUtils.stringToIntegerList(wanYaoMissionConfig.getBeanList(),";"));

                wanYaoMissionConfig.setRewards(rs.getString("rewards"));

                wanYaoMissionConfig.setBossBean(rs.getInt("bossBean"));
                map.put(wanYaoMissionConfig.getId(), wanYaoMissionConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getWanYaoMonsterKingConfigMapSql = "select * from `t_s_activity_wanyao_king` where `activityId`=?";
    public Map<Integer,WanYaoMonsterKingConfig> getWanYaoMonsterKingConfigMap(int activityId) {
        Map<Integer,WanYaoMonsterKingConfig> map= new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWanYaoMonsterKingConfigMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WanYaoMonsterKingConfig wanYaoMonsterKingConfig = new WanYaoMonsterKingConfig();
                wanYaoMonsterKingConfig.setActivityId(rs.getInt("activityId"));
                wanYaoMonsterKingConfig.setLevel(rs.getInt("level"));
                wanYaoMonsterKingConfig.setCost(rs.getInt("cost"));
                wanYaoMonsterKingConfig.setMonsterWeight(rs.getString("xiaoYaoWeights"));

                wanYaoMonsterKingConfig.setMonsterWeightList(new ArrayList<>());
                List<String> weightStringList = StringUtils.stringToStringList(wanYaoMonsterKingConfig.getMonsterWeight(), "\\|");
                for (String weightString : weightStringList) {
                    List<Integer> idWeight = StringUtils.stringToIntegerList(weightString, ";");
                    NormalWeightItem normalWeightItem = new NormalWeightItem(BigInteger.valueOf(idWeight.get(0)), idWeight.get(1));
                    wanYaoMonsterKingConfig.getMonsterWeightList().add(normalWeightItem);
                }

                wanYaoMonsterKingConfig.setXiaoYaoMax(rs.getInt("xiaoYaoMax"));
                wanYaoMonsterKingConfig.setXiaoYaoMin(rs.getInt("xiaoYaoMin"));
                wanYaoMonsterKingConfig.setStepCountMax(rs.getInt("stepCountMax"));
                wanYaoMonsterKingConfig.setEnergyCost(rs.getInt("energyCost"));
                map.put(wanYaoMonsterKingConfig.getLevel(),wanYaoMonsterKingConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getWanYaoNpcConfigSql = "select * from `t_s_activity_wanyao_army` where `activityId`=?";
    public Map<Integer,WanYaoNpcConfig> getWanYaoNpcConfigList(int activityId) {
        Map<Integer,WanYaoNpcConfig>  map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWanYaoNpcConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WanYaoNpcConfig wanYaoNpcConfig = new WanYaoNpcConfig();
                wanYaoNpcConfig.setActivityId(rs.getInt("activityId"));
                wanYaoNpcConfig.setId(rs.getInt("id"));
                wanYaoNpcConfig.setIcon(rs.getInt("icon"));
                wanYaoNpcConfig.setName(rs.getString("name"));
                wanYaoNpcConfig.setDesc(rs.getString("desc"));
                wanYaoNpcConfig.setAtk(rs.getInt("atk"));
                map.put(wanYaoNpcConfig.getId(),wanYaoNpcConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }









}
